/**
 * Created by Administrator on 2017/2/8.
 */

$(function () {
    var key = getCookie('key');
    var store_id = getCookie('store_id');
    if (!key) {
        window.location.href = WapSiteUrl + '/tmpl/member/login.html';
    }
    $.sValid.init({
        rules:{
            store_name:"required",
            letter:"required"
        },
        messages:{
            store_name:"店铺名称必须填写！",
            letter:"店铺缩写必须填写!"
        },
        callback:function (eId,eMsg,eRules){
            if(eId.length >0){
                var errorHtml = "";
                $.map(eMsg,function (idx,item){
                    errorHtml += "<p>"+idx+"</p>";
                });
                errorTipsShow(errorHtml);
            }else{
                errorTipsHide();
            }
        }
    });

    var allow_submit = true;
    $('.footer').click(function(){//会员登陆
        if (allow_submit) {
            allow_submit = false;
        } else {
            return false;
        }
        var store_name = $('#store_name').val();
        var store_letters = $('#letter').val();
        var store_avatar = $('#imgs').val();
        var store_description = $('#description').val();
        if($.sValid()){
            $.ajax({
                type:'post',
                url:ApiUrl+"/index.php?act=store&op=store_setting",
                data:{key: key,store_name:store_name,store_letters:store_letters,store_avatar:store_avatar,store_description:store_description},
                dataType:'json',
                success:function(result){
                    allow_submit = true;
                    $.sDialog({
                        skin:"red",
                        content:result.datas.msg,
                        okBtn:true,
                        "okBtnText": "确定", //确定按钮的文字
                    });
                }
            });
        } else {
            allow_submit = true;
            if (store_name == '') {
                $.sDialog({
                    skin:"red",
                    content:'店铺名称必须填写',
                    okBtn:true,
                    "okBtnText": "确定", //确定按钮的文字
                });
                return;
            }
            if (store_letters == '') {
                $.sDialog({
                    skin:"red",
                    content:'名称首字母缩写必须填写',
                    okBtn:true,
                    "okBtnText": "确定", //确定按钮的文字
                });
                return;
            }
        }
    });

//初始化页面
//     initPage();
    get_footer();

});
function get_footer() {
    if (!footer) {
        footer = true;
        $.ajax({
            url: WapSiteUrl + '/js/tmpl/footer.js',
            dataType: "script"
        });
    }
}

function upload(obj) {
    var _self = obj;
    var _this = $(obj);
    var fileReader = new FileReader();
    fileReader.onload = function (e) {
        if (fileReader.readyState == FileReader.DONE) {
            var file = _self.files[0];
            var size = file.size;
            var width = file.width;
            var height = file.height;
            var param_num = _this.attr('num');//上传图片数量
            var param_count = _this.attr('count');//已上传图片数量
            $("#avatar").attr('src',this.result);
            $("#imgs").val(this.result);
        }
    };
    //一次加载多个文件
    var i = 0, src = _self.files;
    fileReader.readAsDataURL(src[i]);
    fileReader.onloadend = function () {
        i++;
        if (i < src.length) fileReader.readAsDataURL(src[i]);
    }
}